package com.zyx.flink.realtime.utils;

import com.zyx.flink.realtime.pojo.TransientSink;
import org.apache.flink.connector.jdbc.JdbcConnectionOptions;
import org.apache.flink.connector.jdbc.JdbcExecutionOptions;
import org.apache.flink.connector.jdbc.JdbcSink;
import org.apache.flink.connector.jdbc.JdbcStatementBuilder;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;

import java.lang.reflect.Field;

import static com.zyx.flink.realtime.cons.ClickHouseConst.CLICKHOUSE_URL;

/**
 * @author zyx
 * @since 2021/6/12 8:43
 * desc: 操作ClickHouse的工具类
 */
public class ClickHouseUtil {
    /**
     * 获取向Clickhouse中写入数据的SinkFunction
     *
     * @param sql 插入clickhouse的sql语句
     * @param <T> 流中数据的类型
     * @return 向Clickhouse中写入数据的SinkFunction
     */
    public static <T> SinkFunction<T> getJdbcSink(String sql) {
        //obj  就是流中的一条数据对象
        return JdbcSink.sink(
                sql,
                // 执行写入操作
                // 将当前流中的对象属性赋值给SQL的占位符 insert into user_info(?,?,?,?,?)
                (JdbcStatementBuilder<T>) (ps, obj) -> {
                    //获取当前类中  所有的属性
                    Field[] fields = obj.getClass().getDeclaredFields();
                    //跳过的属性计数
                    int skipOffset = 0;

                    for (int i = 0; i < fields.length; i++) {
                        Field field = fields[i];
                        //通过属性对象获取属性上是否有@TransientSink注解
                        TransientSink transientSink = field.getAnnotation(TransientSink.class);
                        //如果transientSink不为空，说明属性上有@TransientSink标记，那么在给?占位符赋值的时候，应该跳过当前属性
                        if (transientSink != null) {
                            skipOffset++;
                            continue;
                        }
                        //设置私有属性可访问
                        field.setAccessible(true);
                        try {
                            //获取属性值
                            Object o = field.get(obj);
                            ps.setObject(i + 1 - skipOffset, o);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                },
                // 构建者设计模式，创建JdbcExecutionOptions对象，给batchSize属性赋值，执行执行批次大小
                new JdbcExecutionOptions.Builder()
                        .withBatchSize(5)
                        .build(),
                //构建者设计模式，JdbcConnectionOptions，给连接相关的属性进行赋值
                new JdbcConnectionOptions
                        .JdbcConnectionOptionsBuilder()
                        .withUrl(CLICKHOUSE_URL)
                        .withDriverName("ru.yandex.clickhouse.ClickHouseDriver")
                        .build()
        );
    }

}
